package com.cars.grpc.client.account.service;

import com.cars.grpc.hello.HelloRequest;
import com.cars.grpc.hello.MyServiceGrpc;
import io.grpc.StatusRuntimeException;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

/**
 * @author caoqianfan
 * @date 2024/2/6 9:48
 */
@Service
@Slf4j
public class HelloClientService {
    @GrpcClient("grpc-account-server")
    MyServiceGrpc.MyServiceBlockingStub myServiceBlockingStub;
    @GrpcClient("grpc-account-server")
    MyServiceGrpc.MyServiceStub myServiceStub;

    public String sayHello(String name) {
        try {
            return myServiceBlockingStub.sayHello(HelloRequest.newBuilder().setName(name).build()).getMessage();
        } catch (final StatusRuntimeException e) {
            log.error("Error in communication", e);
            return null;
        }
    }

}
